#!/bin/sh

# Doctest-alike IRAF test script. This will take text files with
# MarkDown syntax, extract all code lines (enclosed with ```), execute
# the lines that start with cl> or similar and compare the other lines
# with the output of the IRAF shells (cl, ecl, vocl).
#
# Copyright: 2017 Ole Streicher
# License: as IRAF
#

if [ -z "$iraf" ] ; then
    export iraf=$(cd "$(dirname "$0")/.." && pwd)/
fi
if [ -z "$IRAFARCH" ] ; then
    export IRAFARCH=$("${iraf}unix/hlib/irafarch.sh")
fi

export arch=.${IRAFARCH}
export IRAFBIN=${iraf}bin${arch}/
export PATH=$HOME/.iraf/bin:${PATH}

TEST_FILES=""
IRAF_SHELLS=""
verbose=0

while getopts "h?vc:" opt; do
    case "$opt" in
    h|\?)
	echo 'Run the IRAF test suite. See iraf$test/README.md for details.'
	echo
        echo 'Usage:'
	echo '    run_tests [-h] [-v] [-c CL.e]... [TEST.md]...'
	echo
	echo 'Arguments:'
	echo '    -h       this help'
	echo '    -v       verbose output'
	echo '    -c CL.e  use CL.e as IRAF shell (may be given several times)'
	echo '    TEST.md  run test script TEST.md (may be given several times)'
	echo
	echo 'If no IRAF shell is specified, ecl.e is used.'
	echo 'If no test scripts are specified, all test scripts under iraf$test'
	echo 'are executed.'
        exit 0
        ;;
    v)  verbose=1
        ;;
    c)  IRAF_SHELLS="$IRAF_SHELLS $OPTARG"
        ;;
    esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift

# IRAF shells to be tested
if [ -z "$IRAF_SHELLS" ] ; then
    IRAF_SHELLS="ecl.e"
fi

# Test scripts
for f in $@ ; do
    if [ "$(echo "$f" | cut -c1)" != "/" ] ; then
	f=$(pwd)/$f
    fi
    TEST_FILES="$TEST_FILES $f"
done

if [ -z "$TEST_FILES" ] ; then
    TEST_FILES="${iraf}test/"*.md
fi

base_dir=$(mktemp -d -t iraftest.XXXXXXXXX)
failure_file=${base_dir}/failures.txt
summary_file=${base_dir}/summary.txt

# Run one test. If a failure happens, the full message will be appended to
# the $failure_file.
#
# Parameters:
#
#  - Executable of IRAF shell
#  - Commands to be executed
#  - Expected result string
#  - Test script reference (file name and line number)
#
run_test () {
    local CL="$1"
    local command="$2"
    local result="$3"
    local src="$4"
    local options="$5"
    local skip=$(echo "$options" | tr \  \\n | grep ^skip)
    local xfail=$(echo "$options" | tr \  \\n | grep ^xfail)
    local archs=$(echo "$options" | tr \  \\n | grep ^arch= | cut -d= -f2)
    local decimals=$(echo "$options" | tr \  \\n | grep ^decimals= | cut -d= -f2)
    if [ "$skip" ] ; then
        echo "skipped" >> "$summary_file"
	printf "s"
	return
    fi
    if [ "$archs" ] && [ -z "$(echo "$archs" | tr , \\n | grep ^"$IRAFARCH"\$)" ] ; then
        echo "skipped" >> "$summary_file"
	printf "a"
	return
    fi
    local cmd_file="${base_dir}/cmd"
    printf "%b\\nlogout\\n" "$command" > "$cmd_file"
    res_file="${base_dir}/expected"
    printf "%b\\n" "$result" > "$res_file" 2>&1
    clres_file="${base_dir}/output"
    "${CL}" -f "$cmd_file" > "$clres_file" 2>&1
    diff_file="${base_dir}/diff"
    diff_floatingpoint "$res_file" "$clres_file" "$decimals" > "$diff_file"
    if [ -s "$diff_file" ] ; then
	if [ "$xfail" ] ; then
	    printf "x"
	    echo "xfailed" >> "$summary_file"
	    return
	else
	    cat >> "$failure_file" <<EOF

=================== Failure in $src with $(basename "$CL") ===================

Expected
========
$(cat "$res_file")

Output
======
$(cat "$clres_file")

Diff
====
$(tail -n +3 "$diff_file")
EOF
	    echo "failed" >> "$summary_file"
	    printf "F"
	    return
	fi
    else
        echo "passed" >> "$summary_file"
	printf "."
	return
    fi
    rm -f "$cmd_file" "$res_file" "$clres_file" "$diff_file"
}

# Diff two files, with an optionally given floating point precision
# (in relevant decimals)
#
# Parameters:
#
#  - first file
#  - second file
#  - relevant decimals (optional)
#
diff_floatingpoint () {
    local file1="$1"
    local file2="$2"
    local decimals="$3"
    if [ -z "$decimals" ] ; then
	diff -uw "$file1" "$file2"
    else
	perl -pe 's/(-?\d+\.\d*)(E-?\d+)?/sprintf("%.'"${decimals}"'f%s", $1, $2)/ge;s/\-(0+\.0*)/$1/ge' < "$file1" > "$file1".fp
	perl -pe 's/(-?\d+\.\d*)(E-?\d+)?/sprintf("%.'"${decimals}"'f%s", $1, $2)/ge;s/\-(0+\.0*)/$1/ge' < "$file2" > "$file2".fp
	diff -uw "$file1".fp "$file2".fp
	rm -f "$file1".fp "$file2".fp
    fi
}

# Run all tests for a given IRAF shell and test script
#
# Parameters:
#
#  - Executable of IRAF shell
#  - File name for test script
#
run_tests () {
    local CL="$1"
    FILE="$2"
    in_code=0   # detect whether we are in a code section
    command=''  # collected commands
    result=''   # collected expected results
    filename='' # File name to read in
    options=''  # Test options
    lineno=0    # line counter
    cat "$FILE" | while read -r line ; do
	lineno=$(( lineno + 1 ))
	if [ "$in_code"  = 1 ] ; then
	    if echo "$line" | grep -q '^```$' ; then
		# we are at the end of a code section,
		# so let's execute the test if there was one
		if [ "$command" ] ; then
		    if [ "$verbose" = 1 ] && [ -n "$headline" ] ; then
			printf "\\n    %s: " "$headline"
			headline=""
		    fi
		    run_test "$CL" "$command" "$result" "$(basename "${FILE}"):${l_start}" "$options"
		fi
		command=''
		result=''
		filename=''
		options=''
		in_code=0
	    elif [ "$filename" ] ; then
		# We are in a code line that is to be stored in a file
		printf "%s\\n" "$line" >> "$filename"
	    elif echo "$line" | grep -q -E '^[a-z>]+> ' ; then
		# We are in a code line that contains a command.
		# First add a command to print the full line so that
		# it appears in the output. Then add the command itself
		# And add the command to to the expected results.
		cmd=$(echo "$line" | sed 's/^[a-z>]*> //')
		if [ "$command" ] ; then
		    command="${command}\\nprint '${line}'"
		    result="${result}\\n${line}"
		else
		    command="print '${line}'"
		    result="${line}"
		fi
		command="${command}\\n${cmd}"
	    else
		# We are in a code line that contains the response from IRAF.
		# Just add it to the expected results.
		result="${result}\\n${line}"
	    fi
	elif echo "$line" | grep -q '^```$' ; then
	    # A new code section started. Store the next line number for
	    # reference
	    l_start=$(( lineno + 1 ))
	    in_code=1
	elif echo "$line" | grep -q -E '^File: `.+`$' ; then
	    # There is some file content printed out in the next block.
	    # Just prepare to save it.
	    filename="$(echo "$line" | cut '-d`' -f2)"
	elif echo "$line" | grep -q -E '^Test options: `.+`$' ; then
	    # Special test options
	    options="$(echo "$line" | cut '-d`' -f2)"
	elif echo "$line" | grep -q -E '^#' ; then
	    # Head line
	    headline="$(echo "$line" | sed s/^#*\ //)"
	else
	    filename=""
	    options=""
	fi
    done
    printf "\\n"
}

for CL in $IRAF_SHELLS ; do
    if [ -x "${IRAFBIN}${CL}" ] ; then
	for t in $TEST_FILES ; do
	    test_dir="${base_dir}/testroot"
	    mkdir -p "${test_dir}"
	    printf "%b: %b " "${CL}" "$(basename "${t}")"
	    cd "$test_dir" && run_tests "${IRAFBIN}${CL}" "${t}"
	    rm -rf "${test_dir}"
	done
    else
	echo "IRAF shell $CL not found"
	stat "${IRAFBIN}${CL}" >> "$failure_file" 2>&1
    fi
done

if [ -s "$summary_file" ] ; then
    printf "Test summary: %s\\n" "$(sort "$summary_file" | uniq -c | sed ':a;N;$!ba;s/\\n */, /g')"
fi

if [ -s "$failure_file" ] ; then
    cat "$failure_file"
    rm -rf "${base_dir}"
    exit 1
fi

rm -rf "${base_dir}"
exit 0
